package com.tmc.dao.impl.inthotel;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.tmc.bean.common.Pagination;
import com.tmc.bean.inthotel.hotel.City;
import com.tmc.bean.inthotel.hotel.Country;
import com.tmc.bean.inthotel.hotel.Hotel;
import com.tmc.bean.inthotel.hotel.HotelVoucherRemark;
import com.tmc.dao.impl.BaseDaoImpl;
import com.tmc.util.ObjectUtil;

@Repository
public class HotelDaoImpl extends BaseDaoImpl {

	public void saveAllHotel(List<Hotel> hotels){
		Session session = getSession();
    	session.beginTransaction();
		for (int i = 0; i < hotels.size(); i++) {
			session.save(hotels.get(i));
		}
		session.getTransaction().commit();
	}
	
	public void updateAllHotel(List<Hotel> hotels){
		Session session = getSession();
    	session.beginTransaction();
		for (int i = 0; i < hotels.size(); i++) {
			session.update(hotels.get(i));
		}
		session.getTransaction().commit();
	}
	
	public List<Hotel> findAllHotel(){
		Session session = getSession();
		String hql = "from Hotel h where h.enabledFlag = 'Y' and h.switchs =0 ";
		Query query = session.createQuery(hql);
		List<Hotel> list =(List<Hotel>)query.list();
		return list;
	}
	
	public List<Hotel> findEachVendorHotel(Integer source){
		Session session = getSession();
		String hql = "from Hotel h where h.source = :source ";
		Query query = session.createQuery(hql);
		query.setInteger("source",source);
		List<Hotel> list = query.list();
		return list;
	}
	
	public void selectAndInsert(List<HotelVoucherRemark> hotelVoucherRemarks){
		Session session = getSession();
    	session.beginTransaction();
    	for (int i = 0; i < hotelVoucherRemarks.size(); i++) {
    		System.out.println(hotelVoucherRemarks.get(i).getForeignId());
			String hql = "from Hotel as h where h.hotelId = :hotelId";
			Query query = session.createQuery(hql);
    		query.setInteger("hotelId", hotelVoucherRemarks.get(i).getForeignId());
    		Hotel hotel = (Hotel) query.uniqueResult();
    		if(hotel == null){
    			hotel = new Hotel();
    			hotel.setHotelId(hotelVoucherRemarks.get(i).getForeignId());
    			session.save(hotel);
    		}
		}
	}
	
	public List<Hotel> findHotelByHotelName(String hotelName,String hotelNameCN,Integer source){
		Session session = getSession();
		String hql = "from Hotel h where h.hotelName = :hotelName or h.hotelNameCN = :hotelNameCN and h.source = :source";
		Query query = session.createQuery(hql);
		query.setString("hotelName",hotelName);
		query.setString("hotelNameCN",hotelNameCN);
		query.setInteger("source",source);
		List<Hotel> hotels = (List<Hotel>)query.list();
		return hotels;
	}
	
	public Hotel findHotelById(Integer id){
		Session session = getSession();
		String hql = "from Hotel h where h.id = :id ";
		Query query = session.createQuery(hql);
		query.setInteger("id",id);
		Hotel hotel = (Hotel)query.uniqueResult();
		session.close();
		return hotel;
	}
	
	public Hotel findHotelByHotelId(Integer hotelId){
		Session session = getSession();
		String hql = "from Hotel h where h.hotelId = :hotelId ";
		Query query = session.createQuery(hql);
		query.setInteger("hotelId",hotelId);
		Hotel hotel = (Hotel)query.uniqueResult();
		return hotel;
	}
	
	public List<Hotel> findHotel(Hotel hotel){
		StringBuilder sql = new StringBuilder();
		sql.append(" from Hotel h where h.enabledFlag = 'Y' ");
		if(ObjectUtil.hasText(hotel.getHotelName())){
			Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
    		Matcher m = p.matcher(hotel.getHotelName());
    		if(m.find()){
    			sql.append(" and h.hotelNameCN like :hotelName ");
    		}else{
    			sql.append(" and h.hotelName like :hotelName ");
    		}
		}
		if(ObjectUtil.hasText(hotel.getCityNameCN())){
			Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
    		Matcher m = p.matcher(hotel.getCityNameCN());
    		if(m.find()){
    			sql.append(" and h.cityNameCN =:cityName ");
    		}else{
    			sql.append(" and h.cityName =:cityName ");
    		}
		}
		sql.append(" order by h.hotelId desc ");
		Query query = getSession().createQuery(sql.toString());
		if(ObjectUtil.hasText(hotel.getHotelName())){
			query.setString("hotelName", hotel.getHotelName());
		}
		if(ObjectUtil.hasText(hotel.getCityNameCN())){
			query.setString("cityName", hotel.getCityNameCN());
		}
		return query.list();
	}
	
	public Pagination<Country> findCountryByCon(Hotel hotel,Integer pageIndex,Integer pageSize){
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from Country c where c.enabledFlag = 'Y' ");
		if(ObjectUtil.hasText(hotel.getHotelName())){
			Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
			Matcher m = p.matcher(hotel.getHotelName());
			if(m.find()){
				sql.append(" and c.countryNameCN like :hotelName ");
				params.put("hotelName", "%" + hotel.getHotelName() + "%");
			}else{
				sql.append(" and c.countryName like :hotelName ");
				params.put("hotelName", "%" + hotel.getHotelName() + "%");
			}
		}
		return this.findPagination(sql.toString(),params, pageIndex, pageSize);
	}
	
	public Pagination<City> findCityByCon(Hotel hotel,Integer pageIndex,Integer pageSize){
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from City c where c.enabledFlag = 'Y' ");
		if(ObjectUtil.hasText(hotel.getHotelName())){
			Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
			Matcher m = p.matcher(hotel.getHotelName());
			if(m.find()){
				sql.append(" and c.cityNameCN like :hotelName ");
				params.put("hotelName", "%" + hotel.getHotelName() + "%");
			}else{
				sql.append(" and c.cityName like :hotelName ");
				params.put("hotelName", "%" + hotel.getHotelName() + "%");
			}
		}
		return this.findPagination(sql.toString(), params, pageIndex, pageSize);
	}
	
	public Pagination<Hotel> findHotelByCon(Hotel hotel,Integer pageIndex,Integer pageSize){
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from Hotel h where h.enabledFlag = 'Y' ");
		if(ObjectUtil.hasText(hotel.getHotelName())){
			Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
			Matcher m = p.matcher(hotel.getHotelName());
			if(m.find()){
				sql.append(" and h.hotelNameCN like :hotelName ");
				params.put("hotelName", "%" + hotel.getHotelName() + "%");
			}else{
				sql.append(" and h.hotelName like :hotelName ");
				params.put("hotelName", "%" + hotel.getHotelName() + "%");
			}
		}
		return this.findPagination(sql.toString(), params, pageIndex, pageSize);
	}
	
	public Pagination<Hotel> findByCon(Hotel hotel,Integer pageIndex,Integer pageSize){
		Map<String, Object> params = new HashMap<String, Object>();
    	StringBuilder sql = new StringBuilder();
    	sql.append(" from Hotel h where h.enabledFlag = 'Y' ");
    	if(ObjectUtil.hasText(hotel.getHotelName())){
    		Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
    		Matcher m = p.matcher(hotel.getHotelName());
    		if(m.find()){
    			sql.append(" and h.hotelNameCN like :hotelName or h.cityNameCN like :hotelName or h.countryNameCN like :hotelName ");
    			params.put("hotelName", "%" + hotel.getHotelName() + "%");
    		}else{
    			sql.append(" and h.hotelName like :hotelName or h.cityName like :hotelName or h.countryName like :hotelName ");
    			params.put("hotelName", "%" + hotel.getHotelName() + "%");
    		}
		}
    	/*if(ObjectUtil.hasText(hotel.getCityName())){
    		Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
    		Matcher m = p.matcher(hotel.getCityName());
    		if(m.find()){
    			sql.append(" and h.cityNameCN =:cityName ");
    			params.put("cityName",hotel.getCityName());
    		}else{
    			sql.append(" and h.cityName =:cityName ");
    			params.put("cityName",hotel.getCityName());
    		}
    	}*/
    	sql.append(" order by h.hotelId desc ");
    	return this.findPagination(sql.toString(),params, pageIndex, pageSize);
		
	}
	
	public Pagination<Hotel> findByConName(Hotel hotel,Integer pageIndex,Integer pageSize){
		Map<String, Object> params = new HashMap<String, Object>();
    	StringBuilder sql = new StringBuilder();
    	sql.append(" from Hotel h where h.enabledFlag = 'Y' ");
    	if(ObjectUtil.hasText(hotel.getHotelName())){
    		Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
    		Matcher m = p.matcher(hotel.getHotelName());
    		if(m.find()){
    			sql.append(" and h.hotelNameCN =:hotelName ");
    			params.put("hotelName",hotel.getHotelName());
    		}else{
    			sql.append(" and h.hotelName =:hotelName ");
    			params.put("hotelName",hotel.getHotelName());
    		}
		}
    	sql.append(" order by h.hotelId desc ");
    	return this.findPagination(sql.toString(),params, pageIndex, pageSize);
	}
	
}
